<?php
if (!empty($_FILES)) {
	$tempFile  = $_FILES['file']['tmp_name'];
	$fileParts = pathinfo($_FILES['file']['name']);
	$extension = $fileParts['extension'];

	$targetPath = "/HplusCms/uploads/";

	$targetPath = trim($targetPath,"/") . "/" . date('Y');

	mkdirs($_SERVER['DOCUMENT_ROOT'] ."/". $targetPath . "/");

	$targetFile = "/" . $targetPath . "/" . md5($_FILES['file']['name']) . '.' . $extension;

	if (move_uploaded_file($tempFile, $_SERVER['DOCUMENT_ROOT'] . $targetFile) === true) {
        echo json_encode(array("code" => true,"msg" => "上传成功。","data" => array("src" => $targetFile)));
	} else {
		echo json_encode(array("code" => false,"msg" => "上传失败！"));
	}
}

function mkdirs($dir){
	if(!is_dir($dir)){
		if(!mkdirs(dirname($dir))){
			return false;
		}
		if(!mkdir($dir, 0777)){
			return false;
		}
	}
	return true;
}
?>